[#include "/WEB-INF/pages/common/taglibs.ftl"]

[#if file.contentType == "js" || 
	file.contentType == "css" || 
	file.contentType == "txt" || 
	file.contentType == "bat" || 
	file.contentType == "ini" || file.contentType == "html"]
	[#assign mode="application/xml"/]
	[#if file.contentType == "html"]
		<script src="[@c.url value='/js/plugins/codemirror/css.js'/]"></script>
		<script src="[@c.url value='/js/plugins/codemirror/javascript.js'/]"></script>
		<script src="[@c.url value='/js/plugins/codemirror/xml.js'/]"></script>
		[#assign mode="text/html"/]
	[/#if]
	
	[#if file.contentType == "js"]
		<script src="[@c.url value='/js/plugins/codemirror/javascript.js'/]"></script>
		[#assign mode="text/javascript"/]
	[/#if]
	[#if file.contentType == "css"]
		<script src="[@c.url value='/js/plugins/codemirror/css.js'/]"></script>
		[#assign mode="text/css"/]
	[/#if]

<script type="text/javascript">
	//~ 全屏的resize函数 --------------------------------------------------------
	function isFullScreen(id) {
		//return /\bCodeMirror-fullscreen\b/.test(cm.getWrapperElement().className);
		return /\bCodeMirror-fullscreen\b/.test(document.getElementById(id).className);
	}
	function winHeight() {
		return window.innerHeight
				|| (document.documentElement || document.body).clientHeight;
	}
	// @params：需要全屏的id; 高亮显示cm; 标志位full
	function setFullScreen(id, cm, full) {
		
		//var wrap = cm.getWrapperElement();
		var wrap = document.getElementById(id);
		var textArea = cm.getWrapperElement()
		if (full) {
			
			wrap.className += " CodeMirror-fullscreen";
			//wrap.style.height = winHeight() + "px";
			textArea.style.height = (winHeight() - 160) + "px";
			document.documentElement.style.overflow = "hidden";
			//wrap.style.height = winHeight() + "px";
			
		} else {
			wrap.className = wrap.className.replace(" CodeMirror-fullscreen", "");
			wrap.style.height = "";
			textArea.style.height = "";
			document.documentElement.style.overflow = "";
		}
		cm.refresh();
	}
			
	var base = (function(){
		// 事件绑定
		function _addEventListeners() {
		}
		function _addCodeMirrorListeners() {
			
			var code = document.getElementById("code");
			if (!code) return;
			// 全屏resize事件绑定
			CodeMirror.on(window, "resize",	function() {
				var showing = document.body.getElementsByClassName("CodeMirror-fullscreen")[0];
				if (!showing) return;
				//showing.CodeMirror.getWrapperElement().style.height = winHeight() + "px";
				showing.style.height = winHeight() + "px";
			});
			
			// 定义CodeMirror
			var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
				mode: "${mode}",
				
				lineNumbers: true,
				lineWrapping: true,
				extraKeys: {
	        		"F11": function(cm) {
	        			// 全屏
	          			setFullScreen('fullscreen', cm, !isFullScreen('fullscreen'));
	        	},
	        		"Esc": function(cm) {
	          			if (isFullScreen('fullscreen')) setFullScreen('fullscreen', cm, false);
	        		}
	      		}
			});
			// 选中行高亮
			var hlLine = editor.addLineClass(0, "background", "activeline");
				editor.on("cursorActivity", function() {
				var cur = editor.getLineHandle(editor.getCursor().line);
				if (cur != hlLine) {
					editor.removeLineClass(hlLine, "background", "activeline");
					hlLine = editor.addLineClass(cur, "background", "activeline");
				}
			});		
			//resize按钮单击事件
			$('#resize').unbind().click(function(){
				// 全屏
      			setFullScreen('fullscreen', editor, !isFullScreen('fullscreen'));
			})	
		}
		return {
            config: {
            	url : null,
                theme: null
            },
            init: function () {
            	//设置内容
            	_addEventListeners();
            	_addCodeMirrorListeners();
            }
		}
	}());
	$(document).ready(function() {
		base.config.theme = getTheme();
        //Initializing the form
		base.init();
	});
</script>
[@c.url var="backurl" value="/manage/Resource_list.action" /]
<form style="font-size:14px;" action="[@c.url value='/manage/Resource_update.action'][@c.param name='backurl' value='${backurl}'/][/@c.url]" method="post">
	<input name="path" type="hidden" value="${(file.path)!''}"/>
    <textarea  id="code" name="content" cols="176" style="margin-top:2px;display:none;">${_Utils.toString((file.content)!'')?html}</textarea>
    <div style="clear:both;"></div>
    <button class="stdbtn" style="opacity: 1;">提交</button>
</form>
[#elseif file.contentType == "jpg" || file.contentType == "jpeg" || file.contentType == "png" || file.contentType == "bmp"]
	<div style="overflow:auto;text-align:center;">
		<img style="background-image: -moz-linear-gradient(-90deg, #F7F7F7, #E2E2E2); border: 2px solid #999999; border-radius: 4px 4px 4px 4px; border-color: #FF6600; box-shadow: 1px 1px 12px #AAAAAA;" src="[@c.url value='/${file.path}'/]"/>
	</div>
[/#if]